package com.tian.dataStructure.stack;

public class LinkedListStackDemo {
    public static void main(String[] args) {
        LinkedListStack stack=new LinkedListStack();
        Node node1=new Node(1);
        Node node2=new Node(4);
        Node node3=new Node(3);
        Node node4=new Node(2);
        stack.push(node1);
        stack.push(node2);
        stack.push(node3);
        stack.push(node4);
        stack.show();
        System.out.println("~~~~~~~~~~~~~~~~~~~");
        stack.pop();
        stack.show();
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
        stack.pop();
        stack.show();

        stack.pop();
        stack.pop();
        stack.show();
        System.out.println("~~~~~~~~~~~~~~~");
        stack.pop();
    }

}

class LinkedListStack{
    Node head=new Node(-1);

    public void push(Node node){

        if (head.next==null){
            head.next=node;
            return;
        }
        Node cur=head.next;
        head.next=node;
        node.next=cur;
    }
    public void pop(){
        if (head.next==null){
            System.out.println("链表已空，不能出栈");
            return;
        }
        head.next=head.next.next;
    }
    public void show(){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        Node cur=head.next;
        while (true){
            if (cur==null){
                break;
            }
            System.out.println(cur);
            cur=cur.next;
        }
    }
}


class Node{
    public int date;
    public Node next;

    public Node(int date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Node{" +
                "date=" + date +
                '}';
    }
}

